<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="codec" uri="/WEB-INF/tld/codec.tld"%>
<%@ include file="/WEB-INF/common/taglibs.jsp"%>
<script type="text/javascript"	src="${ctx}/resources/js/datatable-query.js"></script>

<div class="row-fluid">
	<div class="span12">
		<div class="row-fluid">
			<div class="span6">
				<div class="box closed">
					<div class="blue title">
						<h4>
							<span>Upload here ..</span>
						</h4>
						<a href="#" class="minimize">Minimize</a>
					</div>
					<div class="content">

						<form id="uploadForm" method="POST" enctype="multipart/form-data"
							action="${ctx}/admin/template/upload/">
							<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span4" for="textarea">Upload
											File:</label> <input id="file" type="file" name="file" size="30"
											class="ifile"
											onchange="this.form.filePath.value=this.value.substr(this.value.lastIndexOf('\\')+1);" />
									</div>
								</div>
							</div>

							<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span4">Encode </label>
										<div class="span4 controls" id="currencyPair">
											<select name="encoding">
												<option value="UTF-8">UTF-8</option>
												<option value="GBK">GBK</option>
											</select>
										</div>
									</div>
								</div>
							</div>
							<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span4">Auto Mapping </label>
										<div class="span4 controls">
											<input type="checkbox" name="auto" value="true" checked="checked"
												class="styled" style="opacity: 0;">
										</div>
									</div>
								</div>
							</div>
							
							
							<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span4">Ignore Master Analyze (Used for update)</label>
										<div class="span4 controls">
											<input type="checkbox" name="ignore" value="true"  
												class="styled" style="opacity: 0;">
										</div>
									</div>
								</div>
							</div>
 
							<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span4" for="checkboxes">Please
											choose template type</label>

										<div class="span8 controls">

											<div class="left marginT5">
												<div class="checker" id="uniform-undefined">
													<span class=""> <input type="checkbox"
														name="suffixs" value="ftl" class="styled" checked="checked"
														style="opacity: 0;">
													</span>
												</div>
												ftl
											</div>
											<div class="left marginT5">
												<div class="checker" id="uniform-undefined">
													<span class=""> <input type="checkbox"
														name="suffixs" value="html" class="styled" checked="checked"
														style="opacity: 0;"></input>
													</span>
												</div>
												html
											</div>
											<div class="left marginT5">
												<div class="checker" id="uniform-undefined">
													<span class=""> <input type="checkbox"
														name="suffixs" value="htm" class="styled" checked="checked"
														style="opacity: 0;"></input>
													</span>
												</div>
												htm
											</div>
											<div class="left marginT5">
												<div class="checker" id="uniform-undefined">
													<span class=""> <input type="checkbox"
														name="suffixs" value="txt" class="styled" checked="checked"
														style="opacity: 0;"></input>
													</span>
												</div>
												txt
											</div>
										</div>

									</div>
								</div>
							</div>
							<div class="form-actions">
								<button id="upload" class="btn btn-info">Upload</button>
							</div>


						</form>

					</div>
				</div>
			</div>
		</div>



		<div class="box gradient">
			<div class="title">
				<h4>
					<span>Template </span> <span class="box-form right"> <!-- <shiro:hasPermission
	name="template:delete">
	<a href="#" id="del" class="btn btn-mini">Delete</a>
	</shiro:hasPermission> 
	 --> <shiro:hasPermission name="template:edit">
							<a href="#" id="generate" class="btn btn-mini">Generate</a>
						</shiro:hasPermission> <shiro:hasPermission name="template:edit">
							<a href="#" id="synchronize" class="btn btn-mini">Synchronize</a>
							<a href="#" id="scan" class="btn btn-mini">Scan</a>
							 
						</shiro:hasPermission>
					</span>
				</h4>


				<a href="#" class="minimize">Minimize</a>
			</div>

			<div class="content scrollable clearfix">
				<table class="ajaxTable display table table-bordered">
					<thead>
						<tr>
							<th><a id="select_all"><span
									class="icomoon-icon-checkmark-2"></span></a></th>
							<th>Template name</th>
							<th>Template path</th>
							<th>Language</th>
							<th>Inline Edit</th>
							<th>Searchable</th>
							<th>Operation</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</div>
	<script src="${ctx }/resources/js/jquery.form.js"
		type="text/javascript"></script>
	<script type="text/javascript">
		var render = false;

		$(document).ready(function() {
			johnny.oTable = $('.ajaxTable').dataTable({
				"aoColumnDefs" : [ {
					'bSortable' : false,
					'aTargets' : [ 0 ]
				} ],
				"bLengthChange" : true,
				"sAjaxSource" : '${ctx}/admin/template/',
				"fnInitComplete" : function(oSettings, json) {
				}
			});
			var name = $("<input>").attr({
				type : "text",
				field : "name",
				op : "cn",
				placeholder : "Template Name",
				class : "filterdata"
			});
			johnny.initTableQuery(name);
			
			
			var del = {
					id : 'attributeDel',
					text  : 'Del',
					domain : "Attribute",
					operation : "Del",
					url : "${ctx}/admin/template/del/" 			};
			johnny.tableBtn(del);
			
			var online = {
					id : 'unsearchable',
					text  : 'Unsearchable',
					domain : "Template",
					operation : "Online",
					url : "${ctx}/admin/template/unsearchable/"
			};
			johnny.tableBtn(online);
			
			var offline = {
					id : 'searchable',
					text  : 'Searchable',
					domain : "Page",
					operation : "Search",
					url : "${ctx}/admin/template/searchable/"
			};
			johnny.tableBtn(offline);
			
			var enableInline = {
					id : 'enableInline',
					text  : 'Enable Inline',
					domain : "Template",
					operation : "Enable Inline",
					url : "${ctx}/admin/template/enableInline/"
			};
			johnny.tableBtn(enableInline);
			var disableInline = {
					id : 'disableInline',
					text  : 'Disable Inline',
					domain : "Template",
					operation : "Disable Inline",
					url : "${ctx}/admin/template/disableInline/"
			};
			johnny.tableBtn(disableInline);
			
			
		});

		var columnHeader = [ 'id', 'name', 'path', 'language','inlineEdit','search'];
		var QueryData = function(aoData) {
			return johnny.initQueryData(aoData, columnHeader);
		}

		var DataTableForm = function(json, echo) {
			var dataArray = new Array();
			for ( var i = 0; i < json.result.length; i++) {
				var link = "${ctx}/admin/template/" + json.result[i].id + "/edit/";
				var inline = "${ctx}/admin/template/" + json.result[i].id + "/air/";
				var clone = "${ctx}/admin/template/" + json.result[i].id + "/clone/";
				var version = "${ctx}/admin/template/" + json.result[i].id + "/version/";
				
				
			
				var subArray = new Array();
				var value = johnny.getForSelect(json.result[i].id);
				subArray.push(value);
				subArray.push(json.result[i].name);
				subArray.push(json.result[i].path);
				subArray.push(json.result[i].language);
				subArray.push(json.result[i].inlineEdit);
				subArray.push(json.result[i].search);
				//
				if (json.result[i].inlineEdit) {
					var conf = [{
						link : link,
						text : 'Edit Source',
						css   : 'icomoon-icon-pencil'
					},
					{
						link : inline,
						text : 'Inline Edit',
						css   : 'icomoon-icon-eyedropper'
						
					},{
						link : clone,
						text : 'Clone Template',
						css   : 'icomoon-icon-copy-2'
						
					},
					{
						link : version,
						text : 'Version Control',
						css   : 'icomoon-icon-copy-2'
						
					}];
					subArray.push(johnny.getGridLink(conf));
				} else {
					var conf = [{
						link : link,
						text : 'edit',
						css   : 'icomoon-icon-pencil'
					},{
						link : clone,
						text : 'clone',
						css   : 'icomoon-icon-copy-2'
						
					},
					{
						link : version,
						text : 'version',
						css   : 'icomoon-icon-copy-2'
						
					}];
					
					
					subArray.push(johnny.getGridLink(conf));
				}

				dataArray.push(subArray);
			}
			return {
				sEcho : echo,
				iTotalRecords : json.totalCount,
				iTotalDisplayRecords : json.totalCount,
				aaData : dataArray
			};
		}

		$(function() {

			$("#synchronize").click(function() {
				$.ajax({
					url : "${ctx}/admin/template/synchronize/",
					type : "POST",
					data : {
						"_method" : "PUT"
					},
					dataType : "json",
					success : function(data) {
						if (data.state && data.state.indexOf("SUCCESS") != -1) {
							johnny.success("synchronize file success");
							return;
						} else {

							johnny.error("Please try later " + data.message);

						}
					}
				});

				return false;
			});

			
			$("#scan").click(function() {
				$.ajax({
					url : "${ctx}/admin/template/scan/",
					type : "POST",
					data : {
						"_method" : "PUT"
					},
					dataType : "json",
					success : function(data) {
						if (data.state && data.state.indexOf("SUCCESS") != -1) {
							johnny.success("synchronize file success");
							return;
						} else {

							johnny.error("Please try later " + data.message);

						}
					}
				});

				return false;
			});
			
			
			$("#generate").click(function() {

				$.ajax({
					url : "${ctx}/admin/template/generate/",
					type : "POST",
					data : {
						"_method" : "PUT"
					},
					dataType : "json",
					success : function(data) {
						if (data.state && data.state.indexOf("SUCCESS") != -1) {
							johnny.success("generate file success");
							return;
						}

						johnny.error("Please try later");
						location.reload();
					}
				});

				return false;
			});

		});
	</script>
	<button type="button" class="btn btn-info"
		onclick="javascript:history.go(-1);">Back</button>
	<shiro:hasPermission name="template:create">
		<a href="${ctx}/admin/template/create/" class="btn btn-info"> <span
			class="icon12 icomoon-icon-plus white"></span> New
		</a>
	</shiro:hasPermission>
</div>
